/**
 * Created by Dalyn on 2017/7/10.
 */
import React , {Component}from 'react';
import {Icon,message,Select} from 'antd';
const Option = Select.Option;
import {Modal,Button,Input} from 'fly';
import Util from "../../util/Util"

export default class createTeamModal extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            createTeamFlag:false,
            teamTypeHint:"",//input提示
            teamNameHint:"",//input提示
            workerNumHint:"",//input提示
            createTeamHintText:"",//delete提示
            teamType:"",
            teamName:"",
            workerNum:"",
        };
        this.messageFlag = true;
        this.operateFlag = true;
        this.teamType_arr = [
            {id:"1",text:"裁剪班组"},
            {id:"2",text:"缝制班组"},
            {id:"3",text:"后整班组"},
            {id:"4",text:"包装班组"},
        ];
    }
    showModal(flag){
        this.setState({
            createTeamFlag: flag,
        });
    }
    /*取消*/
    createTeamCancel(){
        this.setState({
            createTeamFlag: false,
            teamType:"",
            teamName:"",
            workerNum:"",
        });
    }
    /*确定*/
    createTeamSure(){
        let params = {};
        let {teamType,teamName,workerNum} = this.state;
        let {createTeamType,workGroupId} = this.props;
        //console.log(teamType);
        if(createTeamType == "create"){
            teamName = teamName.replace(/(^\s*)|(\s*$)/g,"");
            if(!teamType){
                this.setState({
                    teamTypeHint:"班组类型不能为空"
                });
                return
            }
            if(!teamName){
                this.setState({
                    teamNameHint:"班组名称不能为空"
                });
                return
            }
            if(!workerNum){
                this.setState({
                    workerNumHint:"工人数量不能为空"
                });
                return
            }
            if(this.operateFlag){
                this.operateFlag = false;
                params.teamType = teamType;
                params.teamName = teamName;
                params.workerNum = workerNum;
                this.props.teamOperate("create",params)
            }
        }else if(createTeamType == "edit"){
            teamName = teamName.replace(/(^\s*)|(\s*$)/g,"");
            if(!teamType){
                this.setState({
                    teamTypeHint:"班组类型不能为空"
                });
                return
            }
            if(!teamName){
                this.setState({
                    teamNameHint:"班组名称不能为空"
                });
                return
            }
            if(!workerNum){
                this.setState({
                    workerNumHint:"工人数量不能为空"
                });
                return
            }
            if(this.operateFlag){
                this.operateFlag = false;
                params.workGroupId = workGroupId;
                params.teamType = teamType;
                params.teamName = teamName;
                params.workerNum = workerNum;
                this.props.teamOperate("create",params)
            }
        }else {//提示
            params.workGroupId = workGroupId;
            this.props.teamOperate("delete",params);
        }
    }
    /*输入框*/
    addInputChange(type,e){
        let this_ = this;
        let text = e.target.value;
        let numReg = /^([0-9]{0,6})$/;
        switch (type){
            case "teamName":
                this.setState({
                    teamName:text,
                    teamNameHint:"",
                });
                break;
            case "workerNum":
                if(text&&!numReg.test(text)||Number(text)>999999){
                    /*this.setState({
                        workerNumHint:"格式错误",
                    });*/
                    return;
                }else {
                    this.setState({
                        workerNum:text,
                        workerNumHint:"",
                    })
                }
                break;
        }
    }
    /*班组类型下拉框*/
    selectTeamType(id){
        this.setState({
            teamType:id,
            teamTypeHint:"",
        })
    }
    render(){
        let {teamType,teamName,workerNum,teamTypeHint,teamNameHint,workerNumHint} = this.state;//会被改变的传值-setState
        let {createTeamType,createTeamHintText,workGroupId} = this.props;//不会被改变的传值--props
        /*if(createTeamType == "edit"){
            teamType = this.props.teamType;
            teamName = this.props.teamName;
            workerNum = this.props.workerNum;
        }*/
        //console.log( createTeamType);
        let createTeamTitle = ()=>{
            if(createTeamType == "create"){
                return "新建班组"
            }else if(createTeamType == "edit"){
                return "修改班组"
            }else if(createTeamType == "delete"){
                return "提示"
            }
        };
        let modal_content = ()=>{
            if(createTeamType == "create"||createTeamType == "edit"){
                return (
                    <div>
                        <div className={"production_facResource_set_input"} ref={"teamTypeBox"}>
                            <span className="materielBom_set_inputspan">班组类型：</span>
                            <Select
                                ref={"teamType"}
                                style={{ width: 200 }}
                                className={teamTypeHint?"order_creat_select select_border_red":"order_creat_select"}
                                dropdownClassName="order_creat_select_drop"
                                //placeholder="请选择班组类型"
                                //notFoundContent="该企业未指定联系人"
                                value={teamType?teamType:"请选择班组类型"}
                                optionFilterProp="children"
                                getPopupContainer={()=>this.refs.teamTypeBox}
                                defaultActiveFirstOption={false}
                                onChange={this.selectTeamType.bind(this)}
                                disabled={createTeamType == "create"?false:true}
                                filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                            >
                                {
                                    this.teamType_arr.map((v,i)=>{
                                        return(
                                            <Option key={v.id} value={v.id}>{v.text}</Option>
                                        )
                                    })
                                }
                            </Select>
                            <p className="production_facResource_wsModal_spanHint">{teamTypeHint}</p>
                        </div>
                        <div className={"production_facResource_set_input"}>
                            <span className="materielBom_set_inputspan">班组名称：</span>
                            <Input
                                type="text"
                                className={teamNameHint?"production_facResource_ctModal_name input_border_red":"production_facResource_ctModal_name"}
                                placeholder ={"请输入班组名称"}
                                value={teamName}
                                ref={"teamName"}
                                onChange={this.addInputChange.bind(this,"teamName")}
                                maxLength="6"
                            />
                            <p className="production_facResource_wsModal_spanHint">{teamNameHint}</p>
                        </div>
                        <div className={"production_facResource_set_input"}>
                            <span className="materielBom_set_inputspan">工人数量：</span>
                            <Input
                                type="text"
                                className={workerNumHint?"production_facResource_ctModal_name input_border_red":"production_facResource_ctModal_name"}
                                placeholder ={"请输入工人数量"}
                                value={workerNum}
                                ref={"workerNum"}
                                onChange={this.addInputChange.bind(this,"workerNum")}
                                maxLength="6"
                            />
                            <p className="production_facResource_wsModal_spanHint">{workerNumHint}</p>
                        </div>
                    </div>
                )
            }else if(createTeamType == "delete"){
                return (
                    <p className="production_facResource_wsModal_hint_text">
                        <Icon type="exclamation-circle"  className="production_facResource_wsModal_warning"/>
                        <span className="production_facResource_wsModal_hint_word">{createTeamHintText}</span>
                    </p>
                )
            }
        };
        return (
            <Modal
                title={createTeamTitle()}
                visible={this.state.createTeamFlag}
                onCancel={this.createTeamCancel.bind(this)}
                onOk={this.createTeamSure.bind(this)}
                //key={createTeamType}
            >
                {modal_content()}
            </Modal>
        )
    }
}